import {
  controller,
  httpGet,
  httpPost,
  requestParam,
  response,
  type Controller,
  requestBody,
  queryParam, httpPut, httpDelete
} from 'inversify-express-utils'
import {
  ApiOperationDelete,
  ApiOperationGet,
  ApiOperationPost,
  ApiOperationPut,
  ApiPath,
  SwaggerDefinitionConstant
} from 'swagger-express-ts'
import express from 'express'
import { VersionModel } from '@/module/VersionModel.ts'
import { inject } from 'inversify'
import { IVersionServiceImpl } from '@/service/system/impl/IVersionServiceImpl.ts'

@ApiPath({
  path: '/versions',
  name: '版本管理'
})
// todo 这边要做一下权限拦截中间件
// (e, a, next) => { console.log(e); next() }
@controller('/versions')
export class VersionController implements Controller {
  // 注入服务
  @inject(Symbol.for('IVersionServiceImpl')) IVersionService: IVersionServiceImpl

  @ApiOperationGet({
    path: '/testGet/{id}',
    summary: '列表',
    parameters: {
      path: {
        id: {
          type: SwaggerDefinitionConstant.NUMBER
        }
      },
      query: {
        name: {
          type: SwaggerDefinitionConstant.STRING
        },
        description: {
          type: SwaggerDefinitionConstant.STRING
        }
      }
    },
    responses: {
      200: { description: 'Success', type: SwaggerDefinitionConstant.Response.Type.ARRAY, model: 'VersionModel' }
    }
  })
  @httpGet('/testGet/:id')
  public getVersions (@requestParam('id') id: number, @queryParam('name') name: string, @response() res: express.Response<VersionModel>) {
    res.json({ id, name, description: '' })
  }

  @ApiOperationPost({
    summary: '详情',
    path: '/testPost',
    parameters: {
      body: { description: 'New version', model: 'VersionModel' }
    },
    responses: {
      200: { description: 'Success', model: 'VersionModel' }
    }
  })
  @httpPost('/testPost')
  public postVersion (@requestBody() body: VersionModel, @response() res: express.Response<VersionModel>) {
    this.IVersionService.getList(body.name)
    res.json(body)
  }

  @ApiOperationPut({
    summary: '修改',
    path: '/testPut',
    parameters: {
      body: { description: 'New version', model: 'VersionModel' }
    },
    responses: {
      200: { description: 'Success', model: 'VersionModel' }
    }
  })
  @httpPut('/testPut')
  public putVersion (@requestBody() body: VersionModel, @response() res: express.Response<VersionModel>) {
    res.json(body)
  }

  @ApiOperationDelete({
    summary: '删除',
    path: '/testDelete/{id}',
    parameters: {
      path: {
        id: {
          type: SwaggerDefinitionConstant.NUMBER
        }
      }
    },
    responses: {
      200: { description: 'Success' }
    }
  })
  @httpDelete('/testDelete/:id')
  public deleteVersion (@requestParam('id') id: number, @response() res: express.Response<VersionModel>) {
    res.json({ id, name: '', description: '' })
  }
}
